﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Xml.Linq;
using CoreShop.Library;
using CoreShop.Model;
using CoreShop.Repository;
using SqlSugar;
using static CoreShop.Library.GlobalEnumVars;

namespace CoreShop.Services
{
    /// <summary>
    /// 接口实现
    /// </summary>
    public class SignalDataServices : Repository<FromBodyModel>, ISignalDataServices
    {

        /// <summary>
        /// 107-商品列表(FOOD)
        /// </summary>
        /// <returns></returns>
        public List<FoodInfoModel> GetFoodList(FoodReqModel entry)
        {
            var where = Expressionable.Create<corecmsfood>();
            where = where.And(f => f.isDelete == false);
            if (entry.foodTypeId > 0) where = where.And(f => f.foodTypeId == entry.foodTypeId);
            if (!string.IsNullOrEmpty(entry.name))
            {
                string sName = entry.name;
                where = where.And(f => f.name.Contains(sName) || f.number.Contains(sName) ||
                    f.byName.Contains(sName) || f.helpCode.Contains(sName) || f.pinYin.Contains(sName));
            }

            var list = new corecmsfoodServices().AsQueryable().LeftJoin<corecmsfoodspec>((f, s) => f.foodSpecId == s.id)
                .Where(where.ToExpression()).Select((f, s) => new FoodInfoModel
                {
                    id = f.id,
                    foodTypeId = f.foodTypeId,
                    number = f.number,
                    name = f.name,
                    specFlag = f.specFlag,
                    specName = s.specName,
                    pinYin = f.pinYin,
                    posPrice = s.posPrice,
                    vipPrice = s.vipPrice,
                    image = f.image,
                }).With(SqlWith.NoLock).ToList();
            return list;
        }

        /// <summary>
        /// 111-商品规格列表
        /// </summary>
        /// <returns></returns>
        public List<FoodSpecModel> GetFoodSpecList(long id)
        {
            var list = new corecmsfoodspecServices().AsQueryable()
                .Where(p => p.foodId == id && p.isDelete == false)
                .Select(p => new FoodSpecModel
                {
                    foodSpecId = p.id,
                    specName = p.specName,
                    posPrice = p.posPrice,
                    vipPrice = p.vipPrice,
                }).With(SqlWith.NoLock).ToList();
            return list;
        }

        /// <summary>
        /// 116-商品分类列表(CPFL)
        /// </summary>
        /// <returns></returns>
        public List<FoodTypeModel> GetFoodCategoryList()
        {
            var list = new corecmsfoodcategoryServices().AsQueryable()
                .Where(p => p.isDelete == false)
                .Select(p => new FoodTypeModel
                {
                    id = p.id,
                    name = p.name
                }).With(SqlWith.NoLock).ToList();
            return list;
        }

        /// <summary>
        /// 101-登录(DL)
        /// </summary>
        /// <returns></returns>
        public UserResponseModel OnLogin(UserRequestModel model)
        {
            var item = new sysuserServices().AsQueryable()
                .Where(p => p.userName == model.userName && p.passWord == model.password)
                .Select(p => new UserResponseModel
                {
                    id = p.id,
                    number = p.userName,
                    name = p.nickName,
                    storeId = p.storeId,
                }).With(SqlWith.NoLock).First();
            return item;
        }

        /// <summary>
        /// 117-餐台汇总
        /// </summary>
        /// <returns></returns>
        public List<DatabaseModel> GetDeskEmptyList()
        {
            var listDesk = new corecmsfooddeskServices().QueryDeskAndStatus(0, (int)GlobalEnumVars.TableStatus.Empty);

            var list = listDesk.Select(p => new DatabaseModel
            {
                id = p.id,
                number = p.number,
                name = p.name,
            }).ToList();
            return list;
        }

        /// <summary>
        /// 118-餐台区域列表
        /// </summary>
        /// <returns></returns>
        public List<DatabaseModel> GetDeskCategoryList()
        {
            var DeskCategorys = new corecmsfooddeskcategoryServices().GetList();

            var list = DeskCategorys.Select(p => new DatabaseModel
            {
                id = p.id,
                number = p.number,
                name = p.name,
            }).ToList();
            return list;
        }

        /// <summary>
        /// 132-打印机汇总
        /// </summary>
        /// <returns></returns>
        public List<DatabaseModel> GetPrinterList()
        {
            var data = new corecmsprintschemeServices().AsQueryable()
                .Where(p => p.sendPrintWay == (int)GlobalEnumVars.SendPrintWay.Drive).ToList();

            var list = data.Select(p => new DatabaseModel
            {
                id = p.id,
                number = p.printerIP,
                name = p.printerName,
            }).ToList();
            return list;
        }

        /// <summary>
        /// 128-使用餐台汇总(YTKX)
        /// </summary>
        /// <returns></returns>
        public List<DeskIsUseModel> GetDeskInUseList(long type, int status, long deskId)
        {
            var listDesk = new corecmsfooddeskServices().QueryDeskAndStatus(type, status, "", deskId);

            var list = listDesk.Select(p => new DeskIsUseModel
            {
                id = p.id,
                number = p.number,
                name = p.name,
                deskStatus = p.deskStatus,
                manQty = p.manQty.Value

            }).ToList();

            var orderList = new corecmsfoodorderingmasterServices().QueryListByUnPay();
            foreach (var item in list)
            {
                var orders = orderList.Where(p => p.deskId == item.id).ToList();
                orders.ForEach(p => p.orderIdStr = p.orderId.ToString());
                item.orders = orders;
            }

            return list;
        }

    }
}